package com.tensquare.base.service;

import com.tensquare.base.dao.LabelDao;
import com.tensquare.base.pojo.Label;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import common.util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: Feng.Wang
 * @Company: Zelin.ShenZhen
 * @Description:
 * @Date: Create in 2019/3/22 15:30
 */
@Service
public class LabelService {
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private LabelDao labelDao;

    /**
     * 查询所有
     * @return
     */
    public List<Label> findAll(){
        return labelDao.findAll();
    }

    /**
     * 根据id查询单个对象
     * @param id
     * @return
     */
    public Label findById(String id){
        return labelDao.findById(id).get();
    }

    /**
     * 添加
     * @param label
     */
    public void add(Label label){
        label.setId(idWorker.nextId() + "");
        labelDao.save(label);
    }

    /**
     * 修改
     * @param label
     */
    public void update(Label label){
        labelDao.save(label);
    }

    /**
     * 根据id删除
     * @param id
     */
    public void deleteById(String id){
        labelDao.deleteById(id);
    }

    /**
     * 根据查询条件得到查询规格对象
     * @param searchmap
     * @return
     */
    public Specification<Label> createSpecifincation(Map searchmap){
        //select Student st from student where sname like ''; //HQL查询
        return new Specification<Label>() {
            //where labelname like '%xxxx%'
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                String labelname = searchmap.get("labelname").toString();
                String state = searchmap.get("state").toString();
                String recommend = searchmap.get("recommend").toString();
                List<Predicate> predicateList = new ArrayList<>();
                if(!StringUtils.isEmpty(labelname)){
                    Predicate predicate1 = cb.like(root.get("labelname").as(String.class), "%" + labelname + "%");
                    predicateList.add(predicate1);
                }
                if(!StringUtils.isEmpty(state)){
                    Predicate predicate2 = cb.equal(root.get("state").as(String.class),  state );
                    predicateList.add(predicate2);
                }
                if(!StringUtils.isEmpty(recommend)){
                    Predicate predicate3 = cb.equal(root.get("recommend").as(String.class), recommend );
                    predicateList.add(predicate3);
                }
                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

    /**
     * 根据查询条件进行查询
     * @param searchMap
     * @return
     */
    public List<Label> findSearch(Map searchMap){
       return labelDao.findAll(createSpecifincation(searchMap));
    }

    /**
     * 根据条件查询带分页功能
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public Page<Label> findSearch(Map searchMap, int page, int size){
        //参数1：代表当前页，从0开始计算
        //参数2：代表每页大小
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return labelDao.findAll(createSpecifincation(searchMap),pageRequest);
    }
}
